#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "crypto/types.h"
#include "crypto/tool.h"
#include "crypto/rsa.h"

void printSafeArray(SAFE_array *sa)
{
    printf("cap: %d\t size: %d\n", sa->cap, sa->size);
    for (int i = 0; i < sa->size; i++)
    {
        printf("%4d", sa->data[i]);
    }
    printf("\n");
}

void printSafeArrayFormatInt16(SAFE_array *sa)
{
    printf("INT16 cap: %d\t size: %d\n", sa->cap, sa->size);
    for (int i = 0; i < sa->size; i += 2)
    {
        printf("%10d", *(uint16 *)(sa->data + i));
    }
    printf("\n");
}

void printSafeArrayFormatInt(SAFE_array *sa)
{
    printf("INT format: cap: %d\t size: %d\n", sa->cap, sa->size);
    for (int i = 0; i < sa->size; i += 4)
    {
        printf("%12d", *(uint *)(sa->data + i));
    }
    printf("\n");
}

int main()
{
    char *string = "333333351231";
    int len = strlen(string);
    SAFE_array *sa = (SAFE_array *)malloc(offset(SAFE_array, data) + len);
    memcpy(sa->data, string, len);
    sa->size = len;
    sa->cap = len;

    RSASuit *suit = RSAgetRSASuit(NULL);
    // RSASuit *suit = (RSASuit *)malloc(sizeof(RSASuit));
    // suit->n = 123030211;
    // suit->m = 123007024;
    // suit->e = 257;
    // suit->d = 18187809;
    // suit->p = 8219;
    // suit->q = 14969;
    RSASuitPrint(suit);
    printSafeArray(sa);
    SAFE_array *sae = RSAEncode(suit->n, suit->d, sa);
    printSafeArray(sae);
    printSafeArrayFormatInt(sae);

    SAFE_array *sad = RSADecode(suit->n, suit->e, sae);
    printf("origin: \n");
    printSafeArray(sa);
    printf("decode: \n");
    printSafeArray(sad);

    // uint base = 23069629;
    // uint64 e = 18187809;
    // int N = 123030211;
    // printf("%d", powMod(base, e, N));

    return 0;
}